<template>
  <div class="table-box">
    <ProTable ref="proTable" title="仓库管理列表" row-key="id" :indent="20" :columns="columns" :request-api="getStorehouseList">
      <template #tableHeader>
        <el-button v-auth="'add'" type="primary" :icon="CirclePlus" @click="openDialog('新增', initData)"> 新增仓库 </el-button>
      </template>
      <template #name="scope">
        <el-text class="mr10">{{ scope.row.name }}</el-text>
        <el-tooltip placement="top" v-if="scope.row.isDefault">
          <template #content>
            业务未指定特定仓库时会指定默认仓<br />库，如菜品成本卡未配置扣减仓库<br />时，会自动指定为默认仓库。
          </template>
          <el-tag>默认</el-tag>
        </el-tooltip>
      </template>
      <template #operation="scope">
        <el-button v-auth="'view'" type="primary" link :icon="View" @click="openDialog('查看', scope.row)"> 查看 </el-button>
        <el-button v-auth="'edit'" type="primary" link :icon="EditPen" @click="openDialog('编辑', scope.row)"> 编辑 </el-button>
        <el-button v-auth="'delete'" type="primary" link :icon="Delete" @click="deleteCategory(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <StorehouseDialog ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="storehouse">
import { ref, reactive } from "vue";
import { Inventory } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { getStorehouseList, createStorehouse, editStorehouse, deleteStorehouse } from "@/api/modules/inventory";
import StorehouseDialog from "@/views/inventory/components/StorehouseDialog.vue";
import { CirclePlus, EditPen, Delete, View } from "@element-plus/icons-vue";

const proTable = ref<ProTableInstance>();
// 新建默认数据
const initData = reactive({
  id: 0,
  isDefault: false
});

const columns: ColumnProps<Inventory.ResStorehouseList>[] = [
  { type: "index", label: "序号", width: 80 },
  { sortable: true, prop: "code", label: "编码", minWidth: 120, align: "left" },
  { sortable: true, prop: "name", label: "仓库名称", minWidth: 140, align: "left", search: { el: "input" } },
  { prop: "operation", label: "操作", width: 220, fixed: "right" }
];

const deleteCategory = async (params: Inventory.ResStorehouseList) => {
  await useHandleData(deleteStorehouse, { id: params.id }, `删除【${params.name}】仓库`);
  proTable.value?.getTableList();
};

const dialogRef = ref<InstanceType<typeof MemberGradeDialog> | null>(null);
const openDialog = (title: string, row: Partial<Inventory.ResStorehouseList> = {}) => {
  const params = {
    title,
    row: { ...row },
    isView: title === "查看",
    api: title === "新增" ? createStorehouse : title === "编辑" ? editStorehouse : undefined,
    getTableList: proTable.value?.getTableList
  };
  dialogRef.value?.acceptParams(params);
};
</script>
